home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / menubar.el.z / menubar.el
Encoding:
Text File  |  1998-05-21  |  19.3 KB  |  514 lines

  1. ;; Menubar support.
  2. ;; Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3. ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
  4. ;; Copyright (C) 1995, 1996 Ben Wing.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  20. ;; Free Software Foundation, 59 Temple Place - Suite 330,
  21. ;; Boston, MA 02111-1307, USA.
  22.  
  23. ;;; Synched up with: Not in FSF. (Completely divergent from FSF menu-bar.el)
  24. ;;; Some stuff in FSF menu-bar.el is in x-menubar.el
  25.  
  26. (defvar default-menubar nil)
  27.  
  28. ;; this function is considered "part of the lexicon" by many,
  29. ;; so we'll leave it here.
  30. (defun kill-this-buffer ()    ; for the menubar
  31.   "Kill the current buffer."
  32.   (interactive)
  33.   (kill-buffer (current-buffer)))
  34.  
  35. (defun set-menubar-dirty-flag ()
  36.   "Tell XEmacs that the menubar has to be updated.
  37. NOTE: XEmacs now recognizes when you set a different value for
  38. `current-menubar'.  You *only* need to call this function if you
  39. destructively modify a part of the menubar and don't set `current-menubar'.
  40. Note that all the functions that modify a menu call this automatically."
  41.   (setq-default current-menubar (default-value 'current-menubar)))
  42.  
  43. ;; #### shouldn't this perhaps be `copy-tree'?
  44. (defun set-menubar (menubar)
  45.   "Set the default menubar to be MENUBAR.
  46. See `current-menubar' for a description of the syntax of a menubar."
  47.   (check-menu-syntax menubar t)
  48.   (setq-default current-menubar (copy-sequence menubar)))
  49.  
  50. (defun set-buffer-menubar (menubar)
  51.   "Set the buffer-local menubar to be MENUBAR.
  52. See `current-menubar' for a description of the syntax of a menubar."
  53.   (check-menu-syntax menubar t)
  54.   (make-local-variable 'current-menubar)
  55.   (setq current-menubar (copy-sequence menubar)))
  56.  
  57. (defun check-menu-syntax (menu &optional menubar-p)
  58.   ;; The C code does syntax checking on the value of `current-menubar',
  59.   ;; but it's better to do it early, before things have gotten messed up.
  60.   (if menubar-p
  61.       nil
  62.     (or (stringp (car menu))
  63.     (signal 'error
  64.         (list "menu name (first element) must be a string" menu)))
  65.     ;;(or (cdr menu) (signal 'error (list "menu is empty" menu)))
  66.     (setq menu (cdr menu)))
  67.   (let (menuitem item)
  68.     (while (keywordp (setq item (car menu)))
  69.       (or (memq item '(:config :included :filter :accelerator))
  70.       (signal 'error
  71.           (list "menu keyword must be :config, :included, :accelerator or :filter"
  72.             item)))
  73.       (if (or (not (cdr menu))
  74.           (vectorp (nth 1 menu))
  75.           (keywordp (nth 1 menu)))
  76.       (signal 'error (list "strange keyword value" item (nth 1 menu))))
  77.       (setq menu (nthcdr 2 menu)))
  78.     (while menu
  79.       (setq menuitem (car menu))
  80.       (cond
  81.        ((stringp menuitem)
  82.     (and (string-match "^\\(-+\\|=+\\):\\(.*\\)" menuitem)
  83.          (setq item (match-string 2 menuitem))
  84.          (or (member item '(;; Motif-compatible 
  85.                 "singleLine"
  86.                 "doubleLine"
  87.                 "singleDashedLine"
  88.                 "doubleDashedLine"
  89.                 "noLine"
  90.                 "shadowEtchedIn"
  91.                 "shadowEtchedOut"
  92.                 "shadowEtchedInDash"
  93.                 "shadowEtchedOutDash"
  94.                 ;; non-Motif (Lucid menubar widget only)
  95.                 "shadowDoubleEtchedIn"
  96.                 "shadowDoubleEtchedOut"
  97.                 "shadowDoubleEtchedInDash"
  98.                 "shadowDoubleEtchedOutDash"
  99.                 ))
  100.          (signal 'error (list "bogus separator style in menu item" item)))
  101.          ))
  102.        ((null menuitem)
  103.     (or menubar-p
  104.         (signal 'error (list "nil is only permitted in the top level of menubars"))))
  105.        ((consp menuitem)
  106.     (check-menu-syntax menuitem))
  107.        ((vectorp menuitem)
  108.     (let ((L (length menuitem))
  109.           plistp)
  110.       (and (< L 3)
  111.            (signal 'error
  112.                (list "button descriptors must be at least 3 long"
  113.                  menuitem)))
  114.       (setq plistp (or (>= L 5) (keywordp (aref menuitem 2))))
  115.       (or (stringp (aref menuitem 0))
  116.           (signal 'error
  117.               (list
  118.                "first element of a button must be a string (the label)"
  119.                menuitem)))
  120.       (or plistp
  121.           (< L 4)
  122.           (null (aref menuitem 3))
  123.           (stringp (aref menuitem 3))
  124.           (signal 'error
  125.               (list
  126.                "fourth element of a button must be a string (the label suffix)"
  127.                menuitem)))
  128.       (if plistp
  129.           (let ((i 2)
  130.             selp
  131.             style
  132.             item)
  133.         (while (< i L)
  134.           (setq item (aref menuitem i))
  135.           (cond ((not (memq item '(:active :suffix :keys :style
  136.                            :full :included :selected
  137.                            :accelerator)))
  138.              (signal 'error
  139.                  (list (if (keywordp item)
  140.                        "unknown menu item keyword"
  141.                      "not a keyword")
  142.                        item menuitem)))
  143.             ((eq item :style)
  144.              (setq style (aref menuitem (1+ i)))
  145.              (or (memq style '(nil toggle radio button text))
  146.                  (signal 'error (list "unknown style" style
  147.                           menuitem))))
  148.             ((eq item :selected) (setq selp t))
  149.             )
  150.           (setq i (+ i (if (eq item :full) 1 2))))
  151.         (if (and selp (not (memq style '(toggle button radio))))
  152.             (signal 'error
  153.                 (list
  154.                  ":selected only makes sense with :style toggle, radio, or button"
  155.                  menuitem)))
  156.         )))
  157.     )
  158.        (t (signal 'error (list "unrecognised menu descriptor" menuitem))))
  159.       (setq menu (cdr menu)))))
  160.  
  161.  
  162. ;;; menu manipulation functions
  163.  
  164. (defun find-menu-item (menubar item-path-list &optional parent)
  165.   "Search MENUBAR for item given by ITEM-PATH-LIST starting from PARENT.
  166. Returns (ITEM . PARENT), where PARENT is the immediate parent of
  167.  the item found.
  168. If the item does not exist, the car of the returned value is nil.
  169. If some menu in the ITEM-PATH-LIST does not exist, an error is signalled."
  170.   (or (listp item-path-list)
  171.       (signal 'wrong-type-argument (list 'listp item-path-list)))
  172.   (or parent (setq item-path-list (mapcar 'normalize-menu-item-name item-path-list)))
  173.   (if (not (consp menubar))
  174.       nil
  175.     (let ((rest menubar)
  176.       result)
  177.       (if (stringp (car rest))
  178.         (setq rest (cdr rest)))
  179.       (while (keywordp (car rest))
  180.     (setq rest (cddr rest)))
  181.       (while rest
  182.     (if (and (car rest)
  183.          (equal (car item-path-list)
  184.             (normalize-menu-item-name (if (vectorp (car rest))
  185.                       (aref (car rest) 0)
  186.                     (if (stringp (car rest))
  187.                     (car rest)
  188.                       (car (car rest)))))))
  189.         (setq result (car rest) rest nil)
  190.       (setq rest (cdr rest))))
  191.       (if (cdr item-path-list)
  192.       (if (consp result)
  193.           (find-menu-item (cdr result) (cdr item-path-list) result)
  194.         (if result
  195.         (signal 'error (list (gettext "not a submenu") result))
  196.           (signal 'error (list (gettext "no such submenu") (car item-path-list)))))
  197.     (cons result parent)))))
  198.  
  199. (defun add-menu-item-1 (leaf-p menu-path new-item before)
  200.   ;; This code looks like it could be cleaned up some more
  201.   ;; Do we really need 6 calls to find-menu-item?
  202.   (when before (setq before (normalize-menu-item-name before)))
  203.   (let* ((item-name
  204.       (cond ((vectorp new-item) (aref new-item 0))
  205.         ((consp   new-item) (car  new-item))
  206.         (t nil)))
  207.      (menubar current-menubar)
  208.      (menu (condition-case ()
  209.            (car (find-menu-item menubar menu-path))
  210.          (error nil)))
  211.      (item-found (cond
  212.               ((null item-name)
  213.                nil)
  214.               ((not (listp menu))
  215.                (signal 'error (list (gettext "not a submenu")
  216.                         menu-path)))
  217.               (menu
  218.                (find-menu-item (cdr menu) (list item-name)))
  219.               (t
  220.                (find-menu-item menubar (list item-name)))
  221.               )))
  222.     (unless menubar
  223.       (error "`current-menubar' is nil: can't add menus to it."))
  224.     (unless menu
  225.       (let ((rest menu-path)
  226.         (so-far menubar))
  227.     (while rest
  228. ;;;      (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
  229.       (setq menu
  230.         (if (eq so-far menubar)
  231.             (car (find-menu-item so-far (list (car rest))))
  232.           (car (find-menu-item (cdr so-far) (list (car rest))))))
  233.       (unless menu
  234.         (let ((rest2 so-far))
  235.           (while (and (cdr rest2) (car (cdr rest2)))
  236.         (setq rest2 (cdr rest2)))
  237.           (setcdr rest2
  238.               (nconc (list (setq menu (list (car rest))))
  239.                  (cdr rest2)))))
  240.       (setq so-far menu)
  241.       (setq rest (cdr rest)))))
  242.     (if (and item-found (car item-found))
  243.     ;; hack the item in place.
  244.     (if menu
  245.         ;; Isn't it very bad form to use nsubstitute for side effects?
  246.         (nsubstitute new-item (car item-found) menu)
  247.       (setq current-menubar (nsubstitute new-item
  248.                          (car item-found)
  249.                          current-menubar)))
  250.       ;; OK, we have to add the whole thing...
  251.       ;; if BEFORE is specified, try to add it there.
  252.       (unless menu (setq menu current-menubar))
  253.       (when before
  254.     (setq before (car (find-menu-item menu (list before)))))
  255.       (let ((rest menu)
  256.         (added-before nil))
  257.     (while rest
  258.       (if (eq before (car (cdr rest)))
  259.           (progn
  260.         (setcdr rest (cons new-item (cdr rest)))
  261.         (setq rest nil added-before t))
  262.         (setq rest (cdr rest))))
  263.     (when (not added-before)
  264.       ;; adding before the first item on the menubar itself is harder
  265.       (if (and (eq menu menubar) (eq before (car menu)))
  266.           (setq menu (cons new-item menu)
  267.             current-menubar menu)
  268.         ;; otherwise, add the item to the end.
  269.         (nconc menu (list new-item))))))
  270.     (set-menubar-dirty-flag)
  271.     new-item))
  272.  
  273. (defun add-menu-button (menu-path menu-leaf &optional before)
  274.   "Add a menu item to some menu, creating the menu first if necessary.
  275. If the named item exists already, it is changed.
  276. MENU-PATH identifies the menu under which the new menu item should be inserted.
  277.  It is a list of strings; for example, (\"File\") names the top-level \"File\"
  278.  menu.  (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
  279. MENU-LEAF is a menubar leaf node.  See the documentation of `current-menubar'.
  280. BEFORE, if provided, is the name of a menu item before which this item should
  281.  be added, if this item is not on the menu already.  If the item is already
  282.  present, it will not be moved."
  283.   (add-menu-item-1 t menu-path menu-leaf before))
  284.  
  285. ;; I actually liked the old name better, but the interface has changed too
  286. ;; drastically to keep it. --Stig 
  287. (defun add-submenu (menu-path submenu &optional before)
  288.   "Add a menu to the menubar or one of its submenus.
  289. If the named menu exists already, it is changed.
  290. MENU-PATH identifies the menu under which the new menu should be inserted.
  291.  It is a list of strings; for example, (\"File\") names the top-level \"File\"
  292.  menu.  (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
  293.  If MENU-PATH is nil, then the menu will be added to the menubar itself.
  294. SUBMENU is the new menu to add.
  295.  See the documentation of `current-menubar' for the syntax.
  296. BEFORE, if provided, is the name of a menu before which this menu should
  297.  be added, if this menu is not on its parent already.  If the menu is already
  298.  present, it will not be moved."
  299.   (check-menu-syntax submenu nil)
  300.   (add-menu-item-1 nil menu-path submenu before))
  301.  
  302. (defun purecopy-menubar (x)
  303.   ;; this calls purecopy on the strings, and the contents of the vectors,
  304.   ;; but not on the vectors themselves, or the conses - those must be
  305.   ;; writable.
  306.   (cond ((vectorp x)
  307.      (let ((i (length x)))
  308.        (while (> i 0)
  309.          (aset x (1- i) (purecopy (aref x (1- i))))
  310.          (setq i (1- i))))
  311.      x)
  312.     ((consp x)
  313.      (let ((rest x))
  314.        (while rest
  315.          (setcar rest (purecopy-menubar (car rest)))
  316.          (setq rest (cdr rest))))
  317.      x)
  318.     (t
  319.      (purecopy x))))
  320.  
  321. (defun delete-menu-item (path)
  322.   "Remove the named menu item from the menu hierarchy.
  323. PATH is a list of strings which identify the position of the menu item in 
  324. the menu hierarchy.  The documentation of `add-submenu' describes menu-paths."
  325.   (let* ((pair (condition-case nil (find-menu-item current-menubar path)
  326.          (error nil)))
  327.      (item (car pair))
  328.      (parent (or (cdr pair) current-menubar)))
  329.     (if (not item)
  330.     nil
  331.       ;; the menubar is the only special case, because other menus begin
  332.       ;; with their name.
  333.       (if (eq parent current-menubar)
  334.       (setq current-menubar (delq item parent))
  335.     (delq item parent))
  336.       (set-menubar-dirty-flag)
  337.       item)))
  338.  
  339. (defun relabel-menu-item (path new-name)
  340.   "Change the string of the specified menu item.
  341. PATH is a list of strings which identify the position of the menu item in 
  342. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  343. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  344. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
  345. NEW-NAME is the string that the menu item will be printed as from now on."
  346.   (or (stringp new-name)
  347.       (setq new-name (signal 'wrong-type-argument (list 'stringp new-name))))
  348.   (let* ((menubar current-menubar)
  349.          (pair (find-menu-item menubar path))
  350.          (item (car pair))
  351.          (menu (cdr pair)))
  352.     (or item
  353.         (signal 'error (list (if menu (gettext "No such menu item")
  354.                                (gettext "No such menu"))
  355.                              path)))
  356.     (if (and (consp item)
  357.              (stringp (car item)))
  358.         (setcar item new-name)
  359.       (aset item 0 new-name))
  360.     (set-menubar-dirty-flag)
  361.     item))
  362.  
  363. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  364. ;;
  365. ;; these are all bad style.  Why in the world would we put evaluable forms
  366. ;; into the menubar if we didn't want people to use 'em?
  367. ;; x-font-menu.el is the only known offender right now and that ought to be
  368. ;; rehashed a bit.
  369. ;; 
  370.  
  371. (defun enable-menu-item-1 (path toggle-p on-p)
  372.   (let (menu item)
  373.     (if (and (vectorp path) (> (length path) 2)) ; limited syntax checking...
  374.         (setq item path)
  375.       (let* ((menubar current-menubar)
  376.              (pair (find-menu-item menubar path)))
  377.         (setq item (car pair)
  378.               menu (cdr pair))
  379.         (or item
  380.             (signal 'error (list (if menu
  381.                                      "No such menu item"
  382.                                    "No such menu")
  383.                                  path)))
  384.         (if (consp item)
  385.             (error "%S is a menu, not a menu item" path))))
  386.     (if (or (> (length item) 4)
  387.             (and (symbolp (aref item 2))
  388.                  (= ?: (aref (symbol-name (aref item 2)) 0))))
  389.         ;; plist-like syntax
  390.         (let ((i 2)
  391.               (keyword (if toggle-p :selected :active))
  392.               (ok nil))
  393.           (while (< i (length item))
  394.             (cond ((eq (aref item i) keyword)
  395.                    (aset item (1+ i) on-p)
  396.                    (setq ok t)))
  397.             (setq i (+ i 2)))
  398.           (cond (ok nil)
  399.                 (toggle-p
  400.                  (signal 'error (list "not a toggle menu item" item)))
  401.                 (t
  402.                  ;; Need to copy the item to extend it, sigh...
  403.                  (let ((cons (memq item menu))
  404.                        (new-item (vconcat item (list keyword on-p))))
  405.                    (if cons
  406.                        (setcar cons (setq item new-item))
  407.                      (if menu
  408.                          (error "couldn't find %S on its parent?" item)
  409.                        (error "no %S slot to set: %S" keyword item)))))))
  410.       ;; positional syntax
  411.       (if toggle-p
  412.           (signal 'error (list "not a toggle menu item" item))
  413.         (aset item 2 on-p)))
  414.     (set-menubar-dirty-flag)
  415.     item))
  416.  
  417. (defun enable-menu-item (path)
  418.   "Make the named menu item be selectable.
  419. PATH is a list of strings which identify the position of the menu item in 
  420. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  421. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  422. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  423.   (enable-menu-item-1 path nil t))
  424.  
  425. (defun disable-menu-item (path)
  426.   "Make the named menu item be unselectable.
  427. PATH is a list of strings which identify the position of the menu item in 
  428. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  429. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  430. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  431.   (enable-menu-item-1 path nil nil))
  432.  
  433. (defun select-toggle-menu-item (path)
  434.   "Make the named toggle- or radio-style menu item be in the `selected' state.
  435. PATH is a list of strings which identify the position of the menu item in 
  436. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  437. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  438. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  439.   (enable-menu-item-1 path t t))
  440.  
  441. (defun deselect-toggle-menu-item (path)
  442.  "Make the named toggle- or radio-style menu item be in the `unselected' state.
  443. PATH is a list of strings which identify the position of the menu item in 
  444. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  445. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  446. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  447.   (enable-menu-item-1 path t nil))
  448.  
  449.  
  450. (defun get-popup-menu-response (menu-desc &optional event)
  451.   "Pop up the given menu and wait for a response.
  452. This blocks until the response is received, and returns the misc-user
  453. event that encapsulates the response.  To execute it, you can do
  454.   (funcall (event-function response) (event-object response))
  455. If no response was received, nil is returned.
  456.  
  457. MENU-DESC and EVENT are as in the call to `popup-menu'."
  458.   ;; partially stolen from w3
  459.   (let ((echo-keystrokes 0)
  460.     new-event)
  461.     (popup-menu menu-desc event)
  462.     (catch 'popup-done
  463.       (while t
  464.     (setq new-event (next-command-event new-event))
  465.     (cond ((misc-user-event-p new-event)
  466.            (throw 'popup-done new-event))
  467.           ((not (popup-up-p))
  468.            (setq unread-command-events (cons new-event
  469.                          unread-command-events))
  470.            (throw 'popup-done nil))
  471.           ((button-release-event-p new-event);; don't beep twice
  472.            nil)
  473.           ((event-matches-key-specifier-p (quit-char))
  474.            (signal 'quit nil))
  475.           (t
  476.            (beep)
  477.            (message "please make a choice from the menu.")))))))
  478.  
  479. (defun popup-menu-and-execute-in-window (menu-desc event)
  480.   "Pop up the given menu and execute its response in EVENT's window.
  481. This blocks until the response is received, temporarily selects
  482. EVENT's window, and executes the command specified in the response.
  483. EVENT can also be a window.  See `popup-menu' for the semantics of
  484. MENU-DESC."
  485.   (let ((response
  486.      (get-popup-menu-response menu-desc
  487.                   (and (eventp event) event))))
  488.     (and (misc-user-event-p response)
  489.      (save-selected-window
  490.        (select-window (if (windowp event) event
  491.                 (event-window event)))
  492.        (funcall (event-function response)
  493.             (event-object response))))))
  494.  
  495. ;; provide default bindings for menu accelerator map
  496. (and (boundp 'menu-accelerator-map)
  497.      (keymapp menu-accelerator-map)
  498.      (progn
  499.        (define-key menu-accelerator-map "\e" 'menu-escape)
  500.        (define-key menu-accelerator-map [left] 'menu-left)
  501.        (define-key menu-accelerator-map [right] 'menu-right)
  502.        (define-key menu-accelerator-map [up] 'menu-up)
  503.        (define-key menu-accelerator-map [down] 'menu-down)
  504.        (define-key menu-accelerator-map [return] 'menu-select)
  505.        (define-key menu-accelerator-map [kp_down] 'menu-down)
  506.        (define-key menu-accelerator-map [kp_up] 'menu-down)
  507.        (define-key menu-accelerator-map [kp_left] 'menu-left)
  508.        (define-key menu-accelerator-map [kp_right] 'menu-right)
  509.        (define-key menu-accelerator-map [kp_enter] 'menu-select)
  510.        (define-key menu-accelerator-map "\C-g" 'menu-quit)))
  511.  
  512.  
  513. (provide 'menubar)
  514.